package com.woniuxy.ticketcinema.controller;

import com.woniuxy.ticketcinema.entity.ResponseResult;
import com.woniuxy.ticketcinema.entity.TicketCinema;
import com.woniuxy.ticketcinema.exception.serviceException.DataException;
import com.woniuxy.ticketcinema.serviec.TicketCinemaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName TicketCinemaController
 * @Description TODO
 * @Author zx
 * @Date 2021-02-24 19:25:35
 * @Version 1.0
 */
@Slf4j
@Api(tags = "电影院管理接口")
@Controller
@RequestMapping("cinema")
public class TicketCinemaController {

    @Resource
    private TicketCinemaService ticketCinemaService;

    @Transactional
    @ResponseBody
    @ApiOperation("添加影院的接口")
    @PostMapping("add")
    public ResponseResult<Void> add(@RequestBody TicketCinema ticketCinema) {
        //插入单个数据
        ticketCinemaService.add(ticketCinema);
        return ResponseResult.SUCCESS;
    }


    @Transactional
    @ResponseBody
    @ApiOperation("批量添加影院的接口")
    @PostMapping("addAll")
    public ResponseResult<Void> addAll(@RequestBody List<TicketCinema> ticketCinemas) {
        //批量插入
        try {
            //无判断
            //ticketCinemaService.saveBatch(ticketCinemas);
            //有判断（影院名是否重复）
            ticketCinemaService.addAll(ticketCinemas);
        } catch (DataException e) {
            return new ResponseResult<>(101, "数据有误");
        }
        return ResponseResult.SUCCESS;
    }


    @ResponseBody
    @ApiOperation("查询所有影院的接口")
    @PostMapping("findAll")
    public ResponseResult<TicketCinema> findAll() {
        return new ResponseResult(200, "查询成功", ticketCinemaService.findAll());
    }

    @ResponseBody
    @ApiOperation("查询单个影院信息")
    @PostMapping("findById")
    public ResponseResult<TicketCinema> findById(@RequestBody Integer id) {
        return new ResponseResult(200, "查询成功", ticketCinemaService.getById(id));
    }


    @ResponseBody
    @ApiOperation("修改影院")
    @PostMapping("updateById")
    public ResponseResult updateById(@RequestBody @Valid TicketCinema ticketCinema) {
        //通过id去修改影院信息
        return null;
    }
}